package org.jeecg.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.wms.entity.WmsSupplier;
import org.jeecg.modules.wms.entity.WmsSupplierContact;
import org.jeecg.modules.wms.entity.WmsSupplierMaterial;
import org.jeecg.modules.wms.mapper.WmsSupplierMapper;
import org.jeecg.modules.wms.service.IWmsSupplierContactService;
import org.jeecg.modules.wms.service.IWmsSupplierMaterialService;
import org.jeecg.modules.wms.service.IWmsSupplierService;
import org.jeecg.modules.wms.vo.WmsPurchaseOrderVO;
import org.jeecg.modules.wms.vo.WmsSupplierVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 供应商信息表
 * @Author: jeecg-boot
 * @Date: 2023-07-18
 * @Version: V1.0
 */
@Service
public class WmsSupplierServiceImpl extends ServiceImpl<WmsSupplierMapper, WmsSupplier> implements IWmsSupplierService {

    @Autowired
    private IWmsSupplierContactService wmsSupplierContactService;
    
    @Autowired
    private IWmsSupplierMaterialService wmsSupplierMaterialService;
    
    @Override
    public WmsSupplierVO getSupplierById(String id) {
        // 获取供应商基本信息
        WmsSupplier supplier = this.getById(id);
        if (supplier == null) {
            return null;
        }
        
        // 封装VO
        WmsSupplierVO supplierVO = new WmsSupplierVO();
        BeanUtils.copyProperties(supplier, supplierVO);
        
        // 获取联系人列表
        List<WmsSupplierContact> contactList = wmsSupplierContactService.listBySupplier(id);
        supplierVO.setContactList(contactList);
        
        // 获取物料关联列表
        List<WmsSupplierMaterial> materialList = wmsSupplierMaterialService.listBySupplier(id);
        supplierVO.setMaterialList(materialList);
        
        return supplierVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveSupplier(WmsSupplierVO wmsSupplierVO) {
        // 保存供应商基本信息
        WmsSupplier supplier = new WmsSupplier();
        BeanUtils.copyProperties(wmsSupplierVO, supplier);
        boolean result = this.save(supplier);
        // 如果有联系人信息，同时添加联系人
//        if (StringUtils.isNotBlank(supplier.getContactPerson()) && StringUtils.isNotBlank(supplier.getContactPhone())) {
//            WmsSupplierContact contact = new WmsSupplierContact();
//            contact.setSupplierId(supplier.getId());
//            contact.setName(supplier.getContactPerson());
//            contact.setPhone(supplier.getContactPhone());
//            contact.setEmail(supplier.getEmail());
//            contact.setIsPrimary(1); // 设为主要联系人
//
//            wmsSupplierContactService.save(contact);
//        }
        
        // 保存联系人列表
        if (wmsSupplierVO.getContactList() != null && !wmsSupplierVO.getContactList().isEmpty()) {
            wmsSupplierContactService.saveBatch(supplier.getId(), wmsSupplierVO.getContactList());
        }
        
        // 保存物料关联列表
        if (wmsSupplierVO.getMaterialList() != null && !wmsSupplierVO.getMaterialList().isEmpty()) {
            wmsSupplierMaterialService.saveBatch(supplier.getId(), wmsSupplierVO.getMaterialList());
        }
        
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSupplier(WmsSupplierVO wmsSupplierVO) {
        // 更新供应商基本信息
        WmsSupplier supplier = new WmsSupplier();
        BeanUtils.copyProperties(wmsSupplierVO, supplier);
        boolean result = this.updateById(supplier);
        
        // 先删除原有联系人，再保存新的联系人
        if (wmsSupplierVO.getContactList() != null) {
            wmsSupplierContactService.deleteBySupplier(supplier.getId());
            if (!wmsSupplierVO.getContactList().isEmpty()) {
                wmsSupplierContactService.saveBatch(supplier.getId(), wmsSupplierVO.getContactList());
            }
        }
        
        // 先删除原有物料关联，再保存新的物料关联
        if (wmsSupplierVO.getMaterialList() != null) {
            wmsSupplierMaterialService.deleteBySupplier(supplier.getId());
            if (!wmsSupplierVO.getMaterialList().isEmpty()) {
                wmsSupplierMaterialService.saveBatch(supplier.getId(), wmsSupplierVO.getMaterialList());
            }
        }
        
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSupplier(String id) {
        // 删除联系人
        wmsSupplierContactService.deleteBySupplier(id);
        
        // 删除物料关联
        wmsSupplierMaterialService.deleteBySupplier(id);
        
        // 删除供应商基本信息
        return this.removeById(id);
    }

    @Override
    public IPage<WmsPurchaseOrderVO> getSupplierPurchaseHistory(String supplierId, Page<WmsPurchaseOrderVO> page) {
        return baseMapper.getSupplierPurchaseHistory(page, supplierId);
    }
} 